package dir

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
)

type FilesList struct {
	Data []*Files `json:"data"`
}

type Files struct {
	Name string `json:"name"`
}

func Read() {
	var fl = &FilesList{}
	files, _ := ioutil.ReadDir("E:/gitee/golang/example/file")
	for _, file := range files {
		fl.Data = append(fl.Data, &Files{file.Name()})
	}
	s, _ := json.Marshal(fl)
	fmt.Println(string(s))
}

func ReadDir() {
	pwd := "E:/gitee/golang/example/file/"

	//获取当前目录下的所有文件或目录信息
	filepath.Walk(pwd, func(path string, info os.FileInfo, err error) error {
		fmt.Println(path)        //打印path信息
		fmt.Println(info.Name()) //打印文件或目录名
		return nil
	})
}
